package com.san.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.base.domain.Tree;
import com.san.common.config.Constant;
import com.san.common.utils.R;
import com.san.system.domain.DeptDO;
import com.san.system.service.DeptService;

/**
 * 部门管理
 * 
 */

@Controller
@RequestMapping("/sys/dept")
public class DeptController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(DeptController.class);

	private String prefix = "system/dept";
	@Autowired
	private DeptService deptService;

	@GetMapping("")
	@RequiresPermissions("sys:dept:view")
	String dept() {
		return prefix + "/view";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:dept:view")
	public List<DeptDO> list() {
		Map<String, Object> query = new HashMap<>(16);
		List<DeptDO> deptList = deptService.list(query);
		return deptList;
	}

	@GetMapping("/add/{pId}")
	@RequiresPermissions("sys:dept:add")
	String add(@PathVariable("pId") Long pId, Model model) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "总部门");
		} else {
			try {
				DeptDO deptDO = (DeptDO) deptService.getById(pId);
				model.addAttribute("pName", deptDO.getName());
			} catch (Exception e) {
				logger.error("获取部门名称失败", e);
			}

		}
		return prefix + "/add";
	}

	@GetMapping("/edit/{deptId}")
	@RequiresPermissions("sys:dept:edit")
	String edit(@PathVariable("deptId") Long deptId, Model model) {
		try {
			DeptDO dept = (DeptDO) deptService.getById(deptId);
			model.addAttribute("dept", dept);

			if (Constant.DEPT_ROOT_ID.equals(dept.getParentId())) {
				model.addAttribute("parentDeptName", "无");
			} else {
				DeptDO parDept = (DeptDO) deptService.getById(dept.getParentId());
				model.addAttribute("parentDeptName", parDept.getName());
			}
		} catch (Exception e) {
			logger.error("获取部门名称失败", e);
		}
		return prefix + "/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:dept:add")
	public R save(DeptDO dept) {
		try {
			if (deptService.save(dept) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("新增部门失败", e);
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/doEdit")
	@RequiresPermissions("sys:dept:edit")
	public R doEdit(DeptDO dept) {
		try {
			if (deptService.doEdit(dept) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("更新部门失败", e);
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("sys:dept:remove")
	public R remove(Long deptId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", deptId);
		if (deptService.count(map) > 0) {
			return R.error(1, "包含下级部门,不允许修改");
		}
		if (deptService.checkDeptHasUser(deptId)) {
			try {
				if (deptService.remove(deptId) > 0) {
					return R.ok();
				}
			} catch (Exception e) {
				logger.error("删除部门失败", e);
			}
		} else {
			return R.error(1, "部门包含用户,不允许修改");
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:dept:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] deptIds) {
		try {
			deptService.batchRemove(deptIds);
		} catch (Exception e) {
			logger.error("批量删除部门失败", e);
		}
		return R.ok();
	}

	@GetMapping("/tree")
	@ResponseBody
	public Tree<DeptDO> tree() {
		Tree<DeptDO> tree = new Tree<DeptDO>();
		tree = deptService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return prefix + "/deptTree";
	}

}
